/*
 * @Author: w
 * @Date: 2022-02-10 22:57:13
 * @Description: 1. 两数之和
 */

// 官方
var twoSum = function (nums, target) {
  let map = new Map();
  for (let i = 0; i < nums.length; i++) {
    if (map.has(target - nums[i])) {
      return [map.get(target - nums[i]), i];
    }
    map.set(nums[i], i);
  }
};

// 我的
var twoSum = function (nums, target) {
  for (let index = 0; index < nums.length; index++) {
    // 不需要创建新变量 element
    const element = nums[index];
    for (let index1 = 0; index1 < nums.length; index1++) {
      // 没必要 continue，判断坐标相同很好
      if (index === index1) {
        continue;
      }
      const element1 = nums[index1];
      if (element + element1 === target) {
        return [index, index1];
      }
    }
  }
  return [0, 0];
};

// 内存
var twoSum = function (nums, target) {
  for (let i = 0; i < nums.length; i++) {
    let other = target - nums[i];
    for (let j = 0; j < nums.length; j++) {
      if (nums[j] === other && i !== j) {
        return [i, j];
      }
    }
  }
};

// 最快
var twoSum = function (nums, target) {
  var jTar = target / 2;
  var num1 = nums.filter(e => {
    return e === jTar;
  });
  if (num1.length > 1) {
    return [nums.indexOf(jTar), nums.indexOf(jTar, nums.indexOf(jTar) + 1)];
  } else {
    var num2 = nums.filter(e => {
      return e >= jTar;
    });
    var num3 = nums.filter(e => {
      return e < jTar;
    });
    for (let i = 0; i < num2.length; i++) {
      for (let j = 0; j < num3.length; j++) {
        if (num2[i] + num3[j] === target) {
          return [nums.indexOf(num2[i]), nums.indexOf(num3[j])];
        }
      }
    }
  }
};
